package com.aischool.interceptor;

import javax.servlet.http.HttpServletRequest;

import com.aischool.service.AiOrgService;
import com.aischool.service.AiTeacherService;
import com.jfinal.aop.Inject;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

public class TeacherInterceptor implements Interceptor {

	@Inject
	AiOrgService aiOrgService;
	@Inject
	AiTeacherService aiTeacherService;

	@Override
	public void intercept(Invocation inv) {
		Controller controller = inv.getController();
				
		if (!checkParent(controller)) {
			return;
		}
		
		inv.invoke();
	}
	
	private boolean checkParent(Controller controller) {
		
		boolean is_pass = true;
		
		HttpServletRequest request = controller.getRequest();
        //HttpServletResponse response = controller.getResponse();
        
        String surl = request.getRequestURI().toString();
        String contextPath = request.getContextPath();
        surl = surl.substring(contextPath.length());
        
                
        if(StrKit.isBlank(surl) || surl.startsWith("/front/teacher/login")
                || surl.startsWith("/front/teacher/register") ){
            return true;
        }
        
        int login_user_id = controller.getAttrForInt("login_user_id");
        
        
        Record rd = aiTeacherService.getOne(login_user_id);
        
        
		if (rd != null) {
			
			/**
			 * 设置临时变量
			 */
			controller.setAttr("login_teacher_rd", rd);
			controller.setAttr("login_teacher_id", rd.getInt("teacher_id"));
			controller.setAttr("login_class_id", rd.getInt("class_id"));
			

		}
		return is_pass;
	}
}
